package com.lanou.musicbox.utils.fragment_back;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.musicbox.entity.DaoMaster;
import com.lanou.musicbox.entity.DaoSession;
import com.lanou.musicbox.entity.EntityDao;


/**
 * Created by quyan on 15/10/4.
 */
public class DaoSingleton {
    //数据库名
    private static final String DATABASE_NAME = "newsdatas.db";
    private volatile static DaoSingleton instance;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;//数据库管理者
    private DaoSession daoSession;//会话
    private Context context;
    private DaoMaster.DevOpenHelper helper;//帮助类

    private EntityDao entityDao;//轻松一刻表


    private DaoSingleton(Context context){
        this.context = context;

    }

    public static DaoSingleton getInstance(Context context){
        if (instance == null){
            synchronized (DaoSingleton.class){
                if (instance == null) {
                    instance = new DaoSingleton(context);
                }
            }
        }
        return instance;
    }

    public DaoMaster.DevOpenHelper getHelper(){
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context,DATABASE_NAME,null);

        }
        return helper;
    }

    private SQLiteDatabase getDb(){
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    private DaoMaster getDaoMaster(){
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    private DaoSession getDaoSession(){
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }


    public  EntityDao getEntityDao(){
        if (entityDao == null){
            entityDao = getDaoSession().getEntityDao();
        }
        return entityDao;
    }


}


